package pub.cleangao.codegen.enquiry;

import lombok.extern.slf4j.Slf4j;
import pub.cleangao.codegen.sqlexe.DbType;
import pub.cleangao.codegen.GenConfigHolder;
import pub.cleangao.codegen.enquiry.mysql.MySQLEnquiryService;
import pub.cleangao.codegen.enquiry.oracle.OracleEnquiryService;
import pub.cleangao.codegen.enquiry.postgres.PostgresEnquiryService;
import pub.cleangao.codegen.enquiry.sqlserver.SqlServerEnquiryService;
import java.util.HashMap;
import java.util.Map;


@Slf4j
public class EnquiryServiceFactory {

    private static final Map<Integer, EnquiryService> SERVICE_MAP = new HashMap<>(8);
    static {
        SERVICE_MAP.put(DbType.MARIADB.getType(), new MySQLEnquiryService());
        SERVICE_MAP.put(DbType.MYSQL.getType(), new MySQLEnquiryService());
        SERVICE_MAP.put(DbType.ORACLE.getType(), new OracleEnquiryService());
        SERVICE_MAP.put(DbType.SQL_SERVER.getType(), new SqlServerEnquiryService());
        SERVICE_MAP.put(DbType.POSTGRE_SQL.getType(), new PostgresEnquiryService());
    }

    public static TableEnquiry getTableEnquiry(GenConfigHolder config) {
        EnquiryService service = SERVICE_MAP.get(config.getDbType());
        if (service == null) {
            throw new RuntimeException("Not support datasource type [" + config.getDriverClass() + "].");
        }
        log.info("using database type: {}", service.signature());
        return service.getTableEnquiry(config);
    }


}
